<script setup lang="ts">
import { ref, reactive } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { FetchUser } from "@/service/getData"
import router from "@/router";

const loginFormRef = ref(null)
const loading = ref(false)

const { Login } = FetchUser();

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    loading.value = true
    await loginFormRef.value.validate()

    // 这里添加您的登录逻辑
    Login(loginForm.username, loginForm.password)
        .then(data => {
          if (data.code == 0) {
            const token = data.data.token; // 假设 token 在 data 中
            console.log('登录成功');

            localStorage.setItem('token', token);
            router.push('/greenhouse');
          }
        })
  } catch (error) {
    console.error('登录验证失败：', error)
    ElMessage.error('请检查输入信息')
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <h2>智慧大棚实时监测系统登录</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              show-password
          />
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              :loading="loading"
              class="login-button"
              @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
      <div class="login-footer">
        默认用户名：Admin
        默认密码：123456
      </div>
    </div>
  </div>
</template>

<style scoped>
:root, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
}
.login-container {
  width: 100vw; /* 使用视窗宽度单位 */
  height: 100vh; /* 使用视窗高度单位 */
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  /* 确保背景图充满 */
  background-size: cover;
  background-position: center;
  /* 防止内容溢出 */
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
}

.login-box {
  width: 400px;
  padding: 40px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 10px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

.login-box h2 {
  text-align: center;
  color: #333;
  margin-bottom: 30px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
}

:deep(.el-input__wrapper) {
  background-color: rgba(255, 255, 255, 0.8);
}

:deep(.el-form-item) {
  margin-bottom: 25px;
}
.login-footer {
  text-align: center;
  color: black;
  margin-top: 20px;
}
</style>